home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 326-350 / disk_337 / cmanual / menus.lzh / Menus / Example8.c < prev    next >
C/C++ Source or Header  |  1990-01-30  |  17KB  |  434 lines

  1. /* Example8                                                            */
  2. /* Same as Example1 except that we this time will verify any menu      */
  3. /* operations. If the user tries to activate this program's menu we    */
  4. /* check if the position of the pointer, and if it is somewhere at the */
  5. /* top of the window (less than 10 lines down) the menu operation will */
  6. /* continue as normal, else we cancel the menu operation.              */
  7.  
  8.  
  9.  
  10. #include <intuition/intuition.h>
  11.  
  12.  
  13.  
  14. struct IntuitionBase *IntuitionBase;
  15.  
  16.  
  17.  
  18. /*************************************************************************/
  19. /*                         F O U R T H   I T E M                         */
  20. /*************************************************************************/
  21.  
  22. /* The text for the fourth item: */
  23. struct IntuiText my_fourth_text=
  24. {
  25.   2,            /* FrontPen, black. */
  26.   0,            /* BackPen, not used since JAM1. */
  27.   JAM1,         /* DrawMode, do not change the background. */
  28.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  29.                 /* This will leave enough space for the check mark. */
  30.   1,            /* TopEdge, 1 line down. */
  31.   NULL,         /* TextAttr, default font. */
  32.   "Italic",     /* IText, the string. */
  33.   NULL          /* NextItem, no link to other IntuiText structures. */
  34. };
  35.  
  36. /* The MenuItem structure for the fourth item: */
  37. struct MenuItem my_fourth_item=
  38. {
  39.   NULL,            /* NextItem, this is the last item in the list. */
  40.   0,               /* LeftEdge, 0 pixels out. */
  41.   30,              /* TopEdge, 30 lines down. */
  42.   150,             /* Width, 150 pixels wide. */
  43.   10,              /* Height, 10 lines high. */
  44.   ITEMTEXT|        /* Flags, render this item with text. */
  45.   ITEMENABLED|     /*        this item will be enabled. */
  46.   CHECKIT|         /*        it is an attribute item. */
  47.   HIGHCOMP,        /*        complement the colours when highlihted. */
  48.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  49.   (APTR) &my_fourth_text, /* ItemFill, pointer to the text. */
  50.   NULL,            /* SelectFill, nothing since we complement the col. */
  51.   0,               /* Command, no command-key sequence. */
  52.   NULL,            /* SubItem, no subitem list. */
  53.   MENUNULL,        /* NextSelect, no items selected. */
  54. };
  55.  
  56.  
  57.  
  58. /*************************************************************************/
  59. /*                          T H I R D   I T E M                          */
  60. /*************************************************************************/
  61.  
  62. /* The text for the third item: */
  63. struct IntuiText my_third_text=
  64. {
  65.   2,            /* FrontPen, black. */
  66.   0,            /* BackPen, not used since JAM1. */
  67.   JAM1,         /* DrawMode, do not change the background. */
  68.   CHECKWIDTH,   /* LeftEdge, CHECKWIDTH amount of pixels out. */
  69.                 /* This will leave enough space for the check mark. */
  70.   1,            /* TopEdge, 1 line down. */
  71.   NULL,         /* TextAttr, default font. */
  72.   "Underlined", /* IText, the string. */
  73.   NULL          /* NextItem, no link to other IntuiText structures. */
  74. };
  75.  
  76. /* The MenuItem structure for the third item: */
  77. struct MenuItem my_third_item=
  78. {
  79.   &my_fourth_item, /* NextItem, linked to the fourth item. */
  80.   0,               /* LeftEdge, 0 pixels out. */
  81.   20,              /* TopEdge, 20 lines down. */
  82.   150,             /* Width, 150 pixels wide. */
  83.   10,              /* Height, 10 lines high. */
  84.   ITEMTEXT|        /* Flags, render this item with text. */
  85.   ITEMENABLED|     /*        this item will be enabled. */
  86.   CHECKIT|         /*        it is an attribute item. */
  87.   HIGHCOMP,        /*        complement the colours when highlihted. */
  88.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  89.   (APTR) &my_third_text, /* ItemFill, pointer to the text. */
  90.   NULL,            /* SelectFill, nothing since we complement the col. */
  91.   0,               /* Command, no command-key sequence. */
  92.   NULL,            /* SubItem, no subitem list. */
  93.   MENUNULL,        /* NextSelect, no items selected. */
  94. };
  95.  
  96.  
  97.  
  98. /*************************************************************************/
  99. /*                         S E C O N D   I T E M                         */
  100. /*************************************************************************/
  101.  
  102. /* The text for the second item: */
  103. struct IntuiText my_second_text=
  104. {
  105.   2,          /* FrontPen, black. */
  106.   0,          /* BackPen, not used since JAM1. */
  107.   JAM1,       /* DrawMode, do not change the background. */
  108.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  109.               /* This will leave enough space for the check mark. */
  110.   1,          /* TopEdge, 1 line down. */
  111.   NULL,       /* TextAttr, default font. */
  112.   "Bold",     /* IText, the string. */
  113.   NULL        /* NextItem, no link to other IntuiText structures. */
  114. };
  115.  
  116. /* The MenuItem structure for the second item: */
  117. struct MenuItem my_second_item=
  118. {
  119.   &my_third_item,  /* NextItem, linked to the third item. */
  120.   0,               /* LeftEdge, 0 pixels out. */
  121.   10,              /* TopEdge, 10 lines down. */
  122.   150,             /* Width, 150 pixels wide. */
  123.   10,              /* Height, 10 lines high. */
  124.   ITEMTEXT|        /* Flags, render this item with text. */
  125.   ITEMENABLED|     /*        this item will be enabled. */
  126.   CHECKIT|         /*        it is an attribute item. */
  127.   HIGHCOMP,        /*        complement the colours when highlihted. */
  128.   0x00000001,      /* MutualExclude, mutualexclude the first item only. */
  129.   (APTR) &my_second_text, /* ItemFill, pointer to the text. */
  130.   NULL,            /* SelectFill, nothing since we complement the col. */
  131.   0,               /* Command, no command-key sequence. */
  132.   NULL,            /* SubItem, no subitem list. */
  133.   MENUNULL,        /* NextSelect, no items selected. */
  134. };
  135.  
  136.  
  137.  
  138. /*************************************************************************/
  139. /*                          F I R S T   I T E M                          */
  140. /*************************************************************************/
  141.  
  142. /* The text for the first item: */
  143. struct IntuiText my_first_text=
  144. {
  145.   2,          /* FrontPen, black. */
  146.   0,          /* BackPen, not used since JAM1. */
  147.   JAM1,       /* DrawMode, do not change the background. */
  148.   CHECKWIDTH, /* LeftEdge, CHECKWIDTH amount of pixels out. */
  149.               /* This will leave enough space for the check mark. */
  150.   1,          /* TopEdge, 1 line down. */
  151.   NULL,       /* TextAttr, default font. */
  152.   "Plain",    /* IText, the string. */
  153.   NULL        /* NextItem, no link to other IntuiText structures. */
  154. };
  155.  
  156. /* The MenuItem structure for the first item: */
  157. struct MenuItem my_first_item=
  158. {
  159.   &my_second_item, /* NextItem, linked to the second item. */
  160.   0,               /* LeftEdge, 0 pixels out. */
  161.   0,               /* TopEdge, 0 lines down. */
  162.   150,             /* Width, 150 pixels wide. */
  163.   10,              /* Height, 10 lines high. */
  164.   ITEMTEXT|        /* Flags, render this item with text. */
  165.   ITEMENABLED|     /*        this item will be enabled. */
  166.   CHECKIT|         /*        it is an attribute item. */
  167.   CHECKED|         /*        this item is initially selected. */
  168.   HIGHCOMP,        /*        complement the colours when highlihted. */
  169.   0xFFFFFFFE,      /* MutualExclude, mutualexclude all items except the */
  170.                    /*                first one. */
  171.   (APTR) &my_first_text, /* ItemFill, pointer to the text. */
  172.   NULL,            /* SelectFill, nothing since we complement the col. */
  173.   0,               /* Command, no command-key sequence. */
  174.   NULL,            /* SubItem, no subitem list. */
  175.   MENUNULL,        /* NextSelect, no items selected. */
  176. };
  177.  
  178.  
  179.  
  180. /*************************************************************************/
  181. /*                              M E N U                                  */
  182. /*************************************************************************/
  183.  
  184. /* The Menu structure for the first (and only) menu: */
  185. struct Menu my_menu=
  186. {
  187.   NULL,          /* NextMenu, no more menu structures. */
  188.   0,             /* LeftEdge, left corner. */
  189.   0,             /* TopEdge, for the moment ignored by Intuition. */
  190.   50,            /* Width, 50 pixels wide. */
  191.   0,             /* Height, for the moment ignored by Intuition. */
  192.   MENUENABLED,   /* Flags, this menu will be enabled. */
  193.   "Mode",        /* MenuName, the string. */
  194.   &my_first_item /* FirstItem, pointer to the first item in the list. */
  195. };
  196.  
  197.  
  198.  
  199. /* Declare a pointer to a Window structure: */ 
  200. struct Window *my_window;
  201.  
  202. /* Declare and initialize your NewWindow structure: */
  203. struct NewWindow my_new_window=
  204. {
  205.   0,             /* LeftEdge    x position of the window. */
  206.   0,             /* TopEdge     y positio of the window. */
  207.   320,           /* Width       320 pixels wide. */
  208.   100,           /* Height      100 lines high. */
  209.   0,             /* DetailPen   Text should be drawn with colour reg. 0 */
  210.   1,             /* BlockPen    Blocks should be drawn with colour reg. 1 */
  211.   CLOSEWINDOW|   /* IDCMPFlags  The window will give us a message if the */
  212.                  /*             user has selected the Close window gad. */
  213.   MENUPICK|      /*             Or if the user has done a menu operation, */
  214.   MENUVERIFY,    /*             or if the user tries to activate a menu. */
  215.   SMART_REFRESH| /* Flags       Intuition should refresh the window. */
  216.   WINDOWCLOSE|   /*             Close Gadget. */
  217.   WINDOWDRAG|    /*             Drag gadget. */
  218.   WINDOWDEPTH|   /*             Depth arrange Gadgets. */
  219.   WINDOWSIZING|  /*             Sizing Gadget. */
  220.   ACTIVATE,      /*             The window should be Active when opened. */
  221.   NULL,          /* FirstGadget No Custom gadgets. */
  222.   NULL,          /* CheckMark   Use Intuition's default CheckMark. */
  223.   "Style Editor",/* Title       Title of the window. */
  224.   NULL,          /* Screen      Connected to the Workbench Screen. */
  225.   NULL,          /* BitMap      No Custom BitMap. */
  226.   80,            /* MinWidth    We will not allow the window to become */
  227.   30,            /* MinHeight   smaller than 80 x 30, and not bigger */
  228.   300,           /* MaxWidth    than 300 x 200. */
  229.   200,           /* MaxHeight */
  230.   WBENCHSCREEN   /* Type        Connected to the Workbench Screen. */
  231. };
  232.  
  233.  
  234.  
  235. main()
  236. {
  237.   /* Boolean variable used for the while loop: */
  238.   BOOL close_me;
  239.  
  240.   /* Declare a variable in which we will store the IDCMP flag: */
  241.   ULONG class;
  242.   
  243.   /* If we recieve a MENUPICK event, the Code field of the message */
  244.   /* structure will contain the menu number of the first selected item. */
  245.   /* Declare a variable to store the Code value in, and an extra menu */
  246.   /* number variable: */
  247.   USHORT code, menu_number;
  248.   
  249.   /* Declare a MenuItem pointer: */
  250.   struct MenuItem *item;
  251.   
  252.   /* Declare a pointer to an IntuiMessage structure: */
  253.   struct IntuiMessage *my_message;
  254.  
  255.  
  256.  
  257.   /* Before we can use Intuition we need to open the Intuition Library: */
  258.   IntuitionBase = (struct IntuitionBase *)
  259.     OpenLibrary( "intuition.library", 0 );
  260.   
  261.   if( IntuitionBase == NULL )
  262.     exit(); /* Could NOT open the Intuition Library! */
  263.  
  264.  
  265.  
  266.   /* We will now try to open the window: */
  267.   my_window = (struct Window *) OpenWindow( &my_new_window );
  268.   
  269.   /* Have we opened the window succesfully? */
  270.   if(my_window == NULL)
  271.   {
  272.     /* Could NOT open the Window! */
  273.     
  274.     /* Close the Intuition Library since we have opened it: */
  275.     CloseLibrary( IntuitionBase );
  276.  
  277.     exit();  
  278.   }
  279.  
  280.  
  281.  
  282.   /* We have opened the window, and everything seems to be OK. */
  283.  
  284.  
  285.  
  286.   SetMenuStrip( my_window, &my_menu );
  287.   printf("Menustrip connected to window!\n");
  288.  
  289.  
  290.   close_me = FALSE;
  291.  
  292.   /* Stay in the while loop until the user has selected the Close window */
  293.   /* gadget: */
  294.   while( close_me == FALSE )
  295.   {
  296.     /* Wait until we have recieved a message: */
  297.     Wait( 1 << my_window->UserPort->mp_SigBit );
  298.  
  299.     /* As long as we collect messages sucessfully we stay in the loop: */
  300.     while(my_message=(struct IntuiMessage *) GetMsg( my_window->UserPort ))
  301.     {
  302.       /* After we have collected the message we can read it, and save any */
  303.       /* important values which we maybe want to check later: */
  304.       class = my_message->Class;
  305.       code = my_message->Code;
  306.  
  307.  
  308.  
  309.       /* Before we reply we need to see if we have recieved a MENUVERIFY */
  310.       /* message: */
  311.       
  312.       if( class == MENUVERIFY )
  313.       {
  314.         /* Yes, we have recieved a MENUVERIFY message!                    */
  315.         /* The user wants to activate a menu, but the problem is that we  */
  316.         /* do not know if it is our window's menu that will be activated, */
  317.         /* or some other window's menu. We can however check it by        */
  318.         /* examining the Code field of the message. If it is equal to     */
  319.         /* MENUWAITING, it means that it is not your window's menu that   */
  320.         /* will be activated, but if Code is equal to MENUHOT it means it */
  321.         /* is is your window's menu that will be activated!               */
  322.  
  323.         if( code == MENUWAITING )
  324.         {
  325.           /* It is not your window's menu that will be activated! */
  326.           
  327.           /* Your program can take a pause if necessary. You maybe want */
  328.           /* to finish of with some drawings, so your program does not  */
  329.           /* trash any menus. This is especially important if you are   */
  330.           /* using the low-level graphics rutines since they do not     */
  331.           /* bother about, windows etc, and will draw over and destroy  */
  332.           /* anything in their way.                                     */
  333.           
  334.           /* Once the program is ready it should reply the message, and */
  335.           /* the menu will be activated.                                */
  336.         
  337.           printf("Another program's menu will be displayed!\n");
  338.         }
  339.         else
  340.           if( code == MENUHOT )
  341.           {
  342.             /* It is your window's menu that will be activated! */
  343.             
  344.             /* You can now take a pause and finish of with something     */
  345.             /* before you let Intuition activate the menu, or you can    */
  346.             /* even stop the whole menu operation if necessary. If you   */
  347.             /* are writing a paint program you maybe only want the user  */
  348.             /* to be able to activate the menu if the pointer is at the  */
  349.             /* top of the display. That would mean that the user can     */
  350.             /* draw with the right mouse button, and when the user wants */
  351.             /* to make a menu choice, he/she simply moves the pointer to */
  352.             /* the top of the display, and then presses the right mouse  */
  353.             /* button.                                                   */
  354.             
  355.             /* We will now check if the pointer is somewhere at the top  */
  356.             /* of the display: */
  357.             if( my_window->MouseY < 10)
  358.             {
  359.               /* The Y coordinate of the pointer is at least less than   */
  360.               /* 10 lines below the TopEdge of the window.               */
  361.             
  362.               /* The menu operation should continue as soon as possible! */
  363.               printf("OK!\n");
  364.             }
  365.             else
  366.             {
  367.               /* The pointer is below the Title bar of the window! */
  368.               /* Cancel the whole menu operation! */
  369.               
  370.               /* To cancel a menu operation you need to change the Code   */
  371.               /* field to MENUCANCEL. IMPORTANT! Do not change the code   */
  372.               /* variable since it is just a copy of the real Code value. */
  373.               /* What we need to do is to change the real value, and that */
  374.               /* is still OK since we have not replied yet.               */
  375.     
  376.               my_message->Code=MENUCANCEL;
  377.  
  378.               printf("Menu operation canceled!\n");
  379.             }
  380.           }
  381.       }
  382.  
  383.       /* After we have read it we reply as fast as possible: */
  384.       /* REMEMBER! Do never try to read a message after you have replied! */
  385.       /* Some other process has maybe changed it. */
  386.       ReplyMsg( my_message );
  387.  
  388.       /* Check which IDCMP flag was sent: */
  389.       if( class == CLOSEWINDOW )
  390.         close_me=TRUE; /* The user selected the Close window gadget! */  
  391.  
  392.       if(class == MENUPICK)
  393.       {
  394.         printf("\nMenu pick!\n");
  395.         menu_number = code;
  396.         
  397.         while( menu_number != MENUNULL )
  398.         {
  399.           /* Get the address of the item: */
  400.           item = (struct MenuItem *) ItemAddress( &my_menu, menu_number );
  401.  
  402.  
  403.           /* Print out the menu number plus etc: */
  404.           printf("menu_number= %d\n", menu_number );
  405.           printf("MENUNUM = %d\n", MENUNUM(menu_number) );
  406.           printf("ITEMNUM = %d\n", ITEMNUM(menu_number) );
  407.           printf("SUBNUM  = %d\n", SUBNUM(menu_number) );
  408.  
  409.  
  410.           /* Get the following item's menu number: */
  411.           menu_number = item->NextSelect;
  412.         }
  413.       }
  414.     }
  415.   }
  416.  
  417.  
  418.  
  419.   printf("Menustrip removed from window!\n");
  420.   ClearMenuStrip( my_window );
  421.  
  422.  
  423.  
  424.   /* Close the window: */
  425.   CloseWindow( my_window );
  426.  
  427.  
  428.  
  429.   /* Close the Intuition Library since we have opened it: */
  430.   CloseLibrary( IntuitionBase );
  431.   
  432.   /* THE END */
  433. }
  434.